//
//  ViewController.m
//  WebviewBrower
//
//  Created by 于洪东 on 16/5/5.
//  Copyright © 2016年 上海星致金融公司. All rights reserved.
//

#import "ViewController.h"

#import "NFSJSBridgeHelper.h"
#import "WYFWebViewProxyDelegate.h"



//屏幕宽度
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
//屏幕高度
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)


@interface ViewController ()<UIWebViewDelegate>

@property(nonatomic,strong) UIWebView *myWebView;
@property(nonatomic,strong) UIView *toolBars;


@property (nonatomic, strong) NFSJSBridgeHelper *jsBridgeHelper;
@property (nonatomic, strong) WYFWebViewProxyDelegate *bridge;




@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self.view addSubview:self.myWebView];
    [self.view addSubview:self.toolBars];
    
    
    
    //设置objective-c和js的桥接
    self.bridge = [[WYFWebViewProxyDelegate alloc] initWithWebView:self.myWebView webViewDelegate:self];
    self.jsBridgeHelper = [[NFSJSBridgeHelper alloc] initWithWebViewProxyDelegate:self.bridge];
    
    
    
    
    // 设置按钮
    NSArray * title = @[@"停止加载",@"后退",@"前进",@"刷新"];
    for (int i = 0; i<4; i++) {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:title[i] forState:UIControlStateNormal];
        button.frame = CGRectMake(i*SCREEN_WIDTH/4, 0, SCREEN_WIDTH/4, 50);
        [button setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonclick:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = i;
        [self.toolBars addSubview:button];
    }
    
    // 设置webview的URl
    NSURL * url = [NSURL URLWithString:@"https://www.fc18.com.cn/fcapp_v2/Dashboard/Dashboard.html"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.myWebView loadRequest:request];

    
}



#pragma mark - 响应方法
-(void)buttonclick:(UIButton *)sender{
    switch (sender.tag) {
            
        case 0:
            //停止加载
            [self.myWebView stopLoading];
            break;
        case 1:
            //后退
            [self.myWebView goBack];
            break;
        case 2:
            //前进
            [self.myWebView goForward];
            break;
        case 3:
            //刷新
            [self.myWebView reload];
            break;
            
        default:
            break;
    }
    
}


#pragma mark - delegate
-(void)webViewDidStartLoad:(UIWebView *)webView{
    
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    return YES;
}





#pragma mark - set和get方法
-(UIWebView *)myWebView{
    if (_myWebView == nil) {
        _myWebView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64-49)];
        _myWebView.delegate = self;
        _myWebView.backgroundColor = [UIColor lightGrayColor];
    }
    return _myWebView;
}

-(UIView *)toolBars{
    if (_toolBars == nil) {
        _toolBars = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 50, SCREEN_WIDTH, 50)];
        _toolBars.backgroundColor = [UIColor whiteColor];
    }
    return _toolBars;
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}











@end
